package com.itheima.consumer.dao;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA 2023.
 * Description:
 * User: homechen
 * Date: 2024-04-27
 * Time: 13:45
 */

/**
 * 定义延迟消息发送方法
 */
@Component
public class DelayedMessageProducer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Scheduled(fixedDelay = 5000)   // 下一次多久执行
    public void sendDelayedMessage(String message) {
        rabbitTemplate.convertAndSend(delayExchange.EXCHANGE_NAME,
                delayExchange.ROUNTING_KEY,
                message,
                new MessagePostProcessor() {
                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        message.getMessageProperties().setDelay(10 * 1000);
                        return message;
                    }
                });
    }
}
